 /*
 * @description: 商家后台banner管理列表
 * @Author: along
 * @Date: 2020-09-17
 * @Last Modified by: along
 * @Last Modified time: 2020-09-17
 */
<template>
    <div class="container zdMallBannerEdit">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <!-- <div class="header-new-box">
                    <p class="header-new-box-title">banner编号:</p>
                    <el-input
                        v-model="formData.banner_no"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div> -->
                <div class="header-new-box">
                    <p class="header-new-box-title">banner标题:</p>
                    <el-input
                        v-model="formData.title"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">状态:</p>
                    <el-select
                        v-model="formData.status"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <!-- <div class="header-new-box">
                    <p class="header-new-box-title">展示时间:</p>
                    <el-date-picker
                        v-model="date"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="small"
                        @change="getTableData(1)"
                    />
                </div> -->
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="()=>{
                            this.$router.push({
                                name: 'zdMallBannerEdit',
                                query: {
                                    banner_id: ''
                                }
                            })
                        }"
                    >
                        新建banner
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="zdMallBannerEdit"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="image_url"
                label="缩略图"
                :width="70"
                align="left"
            >
                <template slot-scope="scope">
                    <div
                        :style="{backgroundImage: `url(${scope.row.image_url})`, width: '60px', height: '60px', backgroundSize: 'contain',backgroundRepeat: 'no-repeat', backgroundPosition: 'center', cursor: 'pointer'}"
                        @click="fnShowPreview(scope.row.image_url)"
                    />
                    <!-- <img
                        :src="scope.row.image_url"
                        :style="{width: '60px', height: '60px', cursor:'pointer'}"
                        @click="fnShowPreview(scope.row.image_url)"
                    > -->
                </template>
            </el-table-column>
            <el-table-column
                prop="title"
                label="标题"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            />
            <el-table-column
                prop="banner_no"
                label="编号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                :width="150"
            />
            <el-table-column
                prop="jump_url"
                label="跳转链接"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                :width="150"
            >
                <template slot-scope="scope">
                    <p
                        @click="fnOpenUrl(scope.row.jump_url)"
                        class="cursor-font"
                        v-if="scope.row.jump_url"
                    >{{ scope.row.jump_url }}</p>
                    <p v-else>--</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="sort"
                label="排序"
                align="left"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="fnEditSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="time_status"
                label="状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.time_status == 0">未开始</p>
                    <p v-if="scope.row.time_status == 1">开始中</p>
                    <p v-if="scope.row.time_status == 2">已结束</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="上架状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    {{ scope.row.status == 1 ? '已上架': '已下架' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="创建时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                :width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="start_time"
                label="展示时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                :width="190"
            >
                <template slot-scope="scope">
                    <p>开始时间:{{ scope.row.start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}</p>
                    <p>结束时间:{{ scope.row.end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}</p>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
            >
                <template slot-scope="scope">
                    <div class="banner-flex">
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="fnEditBanner(scope.row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            type="text"
                            class="curse-add"
                            v-if="scope.row.status == 0"
                            @click="fnSettingCoupon(scope.row)"
                        >
                            上架
                        </el-button>
                        <el-button
                            type="text"
                            class="curse-add"
                            v-if="scope.row.status == 1"
                            @click="fnSettingCoupon(scope.row)"
                        >
                            下架
                        </el-button>
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="fnDeletebanner(scope.row)"
                        >
                            删除
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            formData: {
                title: '',
                status: 99,
                banner_no: ''
            },
            date:[],
            statusList: [{value: 99,label: '全部'},{value: 1,label: '已上架'},{value: 2,label: '已下架'},{value: 3,label: '已结束'}],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{title: '官网'},{title: '官网自定义'},{title: '商家后台banner'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * @description 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/hire_banner%5Cget_list', {
                page_id: page || this.page.now_page,
                school_filter_status: 99,
                show_status: this.formData.status,
                banner_no: this.formData.banner_no,
                title: this.formData.title
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 排序
         */
        fnEditSort (row) {
            this.$prompt('请输入排序号', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^([1-9]\d?|[1-9]\d+)$/,
                inputErrorMessage: '排序号格式不正确,请重新输入'
            }).then(({ value }) => {
                this.$post('/student/hire_banner%5Cset',{
                    banner_no: row.banner_no,
                    sort: value
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * @description 编辑banner
         */
        fnEditBanner (row) {
            this.$router.push({
                name: 'zdMallBannerEdit',
                query: {
                    banner_id: row.banner_no
                }
            });
        },

        /**
         * @description 删除
         */
        fnDeletebanner (row) {
            this.$confirm('是否要删除该banner', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/hire_banner%5Cdelete',{
                    banner_no: row.banner_no
                },resp => {
                    if(resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * @description 开启/关闭
         */
        fnSettingCoupon (row) {
            this.$post('/student/hire_banner%5Cset_status',{
                banner_no: row.banner_no,
                status: row.status == 1 ? 0 : 1
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success',
                        duration: 2000
                    });
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description
         */
        fnOpenUrl (url) {
            window.open(url, 'target');
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        // justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .banner-flex {
        display: flex;
        flex-direction: column;
        align-items: center;
    }
</style>
<style lang="less">
    .zdMallBannerEdit{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .banner-flex {
            .el-button {
                padding: 5px 0;
                margin-left: 0px;
            }
        }
    }
</style>
